<?php
session_start();
// Connect to the DB.

ini_set('display_errors', 'On');
error_reporting(E_ALL);

$_SESSION["errmsg"] = '';
require_once '../include/config.lib.php';
require_once '../include/database.lib.php';

$paperId = $_SESSION["bibtexid"];
$url = "../paper_view.php?id=". $paperId  ;//."#tabs-2";

function getReplyComment($replyto){    // returns a reply comment based on replytoid

        $comment = DBExecute("    SELECT anno.*, b.title, p.name as comname, b.id AS bid
                            FROM (
                                SELECT a.ratebyanno, a.ID, a.annofabid, a.hasreply, a.resourceid, a.annotationbody, a.personid, a.datecreated AS date, r.bibtexid AS bibtexid
                                FROM annotation a, resource r  
                                WHERE a.replyto = ? AND r.id = a.resourceid
                            ) AS anno
                            JOIN bibtex b ON b.id=anno.bibtexid
                            JOIN person p ON p.id = anno.personid
                            ORDER BY date DESC", array($replyto));
        return $comment;
    }

if(isset($_SESSION["accountNo"])){//user has logged in
	//header("Content-Disposition:attachment;filename=PersonalReport.htm"); //forces a file download
	ConnectToDB();
	$selectid = DBExecute("SELECT id FROM person WHERE username = ?",$_SESSION["accountNo"]); //find the user's id
	$personId = $selectid->fetchAssocRow();

	$username = $_SESSION["accountNo"];
	$username = '%'.$username.'%';
	
	//authors of the publication
	/*$authorsReq = DBExecute(' SELECT unique p.name AS author FROM resource r, person p, authoring a		
						WHERE r.bibtexid = ? and a.resourceid = r.id AND a.personid = p.id', array($paperId));
	$authors = '';	
	while( $keyrow = $authorsReq->fetchAssocRow()) {
		$authors .= $keyrow['AUTHOR'] . '; ';
	}*/
	$authorsReq = DBExecute(' SELECT authors FROM bibtex		
						WHERE id = ? ', array($paperId));
	$authors = ($authorsReq->fetchAssocRow())['AUTHORS'];
	
	
	//load paper summary
	$paperReq = DBExecute('	SELECT title, abstract FROM bibtex b WHERE b.id=?', array($paperId));
	$paper = $paperReq->fetchAssocRow();
	$title = isset($paper['TITLE']) ? $paper['TITLE'] : 'Title not available';	//title
	$abstract = isset($paper['ABSTRACT']) ? $paper['ABSTRACT'] : $paper['URL'];	//abstract
	
	//get comments made on this paper by the user
	$stmt = DBExecute("	SELECT a.*, bibtex.title, person.name as comname, bibtex.id AS bid
							FROM (
								SELECT a.ID, a.ratebyanno, a.id, a.hasreply, a.resourceid, a.annotationbody, a.personid, a.datecreated AS date, r.bibtexid
								FROM annotation AS a JOIN resource AS r ON r.id = a.resourceid
								WHERE r.bibtexid = ? AND a.personid = ? AND a.replyto IS NULL
							) AS a
							JOIN bibtex ON bibtex.id=a.bibtexid
							JOIN person ON person.id = a.personid
							ORDER BY date DESC", array($paperId, $personId['ID']));
	
	
	
	if(0){//this part is not needed right now, maybe useful latter, 
		$_SESSION["errmsg"] = '';
		echo "<HTML><META http-equiv=\"refresh\" content=\"0; url='".$url."'\"></HTML>";//redirect to the papers page
	}
	
	else { //Create the report
		?>
		<div id="report">
			<table id="stat" class="info">
				<td class="labelright"><label for="accountNo"><strong>Title:</strong></label></td>
					<tr><td><?php echo htmlentities($title); ?></td></tr>
					<tr><td><br /></td> </tr>
				<td class="labelright"><label for="accountNo"><strong>Abstract:</strong></label></td>
					<tr><td><?php echo htmlentities($abstract); ?></td></tr>
					<tr><td><br /></td> </tr>
				<td class="labelright"><label for="accountNo"><strong>Authors:</strong></label></td>	
					<tr><td><?php echo htmlentities($authors); ?></td></tr>
					<tr><td><br /></td> </tr>
				<td class="labelright"><label for="accountNo"><strong>Comments:</strong></label></td>	
				<tr>
					<td>
						<?php
						while ($row = $stmt->fetchAssocRow()) { //Display ll main comments made by the user
							$replyCommentID = $row['ID'];							
							$hasReply = $row['HASREPLY'];
							$spacing = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
							?>
								<li>
									<span class="comment_body"><?php									 
									echo htmlentities(preg_replace('/(.*[\n|\r]*.*<content>[\n|\r]*|\s*<\/content>[\n|\r]*.*)/','',$row['ANNOTATIONBODY'])); 
									?></span><br />
									<span class="rating"> <b> Rating: </b> </span>
									<?php
									switch($row['RATEBYANNO']) {
										case 1: echo "Positive"; break;
										case 2: echo "Negative"; break;
										case 4: echo "Informative"; break;
										default: echo "Neutral"; break;
									} ?>
									<br/>
									<b> Made on: </b> <?php echo empty($row['DATE']) ? "None" : $row['DATE'] ?>  <b> By: </b> <?php echo htmlentities$row['COMNAME']) ?>
									<br/>
									<br/>
								</li>
							<?php
							while( $hasReply == 'y'){	//display all the conversations that s

								$reply = getReplyComment($replyCommentID);
						
								while ($newReply = $reply->fetchAssocRow()) {
									?>	
									<?php echo $spacing ?>
										<span class="comment_body"><?php 
										echo htmlentities(preg_replace('/(.*[\n|\r]*.*<content>[\n|\r]*|\s*<\/content>[\n|\r]*.*)/','',$newReply['ANNOTATIONBODY'])); 
										?></span><br />
										<span class="rating">  <?php echo $spacing ?> <b> Rating: </b></span>
										<?php
										switch($newReply['RATEBYANNO']) {
											case 1: echo "Positive"; break;
											case 2: echo "Negative"; break;
											case 4: echo "Informative"; break;
											default: echo "Neutral"; break;
										} ?>
										<br/>
										<?php echo $spacing ?> <b> Made on: </b> <?php echo empty($row['DATE']) ? "None" : $newReply['DATE'] ?> <b> By: </b> <?php echo htmlentities($newReply['COMNAME']) ?>
										<br/>
										<br/>

									<?php
									$spacing = $spacing."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
									$replyCommentID = $newReply['ID'];
									$hasReply = $newReply['HASREPLY'];
								}			
							}
						} ?>
					</td>
				</tr>
			</table>
		</div>
		<?php
	}
}

else{//user hasn't logged in
	$_SESSION["errmsg"] = "You didn't log in.";
	echo "<HTML><META http-equiv=\"refresh\" content=\"0; url=../login.php\"></HTML>";//redirect to the login page
}
?>